package com.oceansoft.mobile.econsole.task.sms;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 时间处理类
 *
 * @author Administrator
 */
public final class DatetimeUtil {
    public static String getCurrentTimeAsString() {
        return getTimeAsString(System.currentTimeMillis());
    }

    public static String getCurrentDateAsString() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat();
        dateFormat.applyPattern("yyyyMMdd");
        return dateFormat.format(new Date(date.getTime()));
    }

    public static String getCurrentDateAsString(String pattern) {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat();
        dateFormat.applyPattern(pattern);
        return dateFormat.format(new Date(date.getTime()));
    }

    public static Date getSysDateAll() {
        return new Date();
    }

    public static Date getSysDateOnly() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String time = df.format(new Date());
        Date date = null;
        try {
            date = df.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Date getSysDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());
        Date date = null;
        try {
            date = df.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String getTimeAsString(long timeMillis, String newFormat) {
        SimpleDateFormat dateFormat = new SimpleDateFormat();
        dateFormat.applyPattern(newFormat);
        return dateFormat.format(new Date(timeMillis));
    }

    public static String getTimeAsString(long timeMillis) {
        return getTimeAsString(timeMillis, "yyyyMMddHHmmss");
    }

    public static long getTimeMillis(String timeString) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat();
        dateFormat.applyPattern("yyyyMMddHHmmss");
        Date dt = dateFormat.parse(timeString);
        return dt.getTime();
    }

    public static String format(long timeMillis) {
        DateFormat dateFormat = DateFormat.getDateTimeInstance(1, 2);
        return dateFormat.format(new Date(timeMillis));
    }

    public static long getTimeMillis(int days, int hour, int minutes) {
        return (days * 24 * 60 * 60 * 1000L + hour * 60 * 60 * 1000L + minutes * 60 * 1000L);
    }

    public static long getDays(long timeMillis) {
        return (timeMillis / 86400000L);
    }

    public static long getHours(long timeMillis) {
        return (timeMillis / 3600000L);
    }

    public static long getMinutes(long timeMillis) {
        return (timeMillis / 60000L);
    }

    public static String getDHM(long timeMillis) {
        long hours = timeMillis % 86400000L;
        long minutes = hours % 3600000L;

        return String.valueOf(getDays(timeMillis)) + "天 " + String.valueOf(getHours(hours)) + "小时: "
                + String.valueOf(getMinutes(minutes)) + "分钟";
    }

    public static int getHourOfDay(long timeMillis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(timeMillis));
        return calendar.get(11);
    }

    public static int getMinite(long timeMillis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(timeMillis));
        return calendar.get(12);
    }

    public static int getSecond(long timeMillis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(timeMillis));
        return calendar.get(13);
    }

    public static String formatDate(Date date, String newFormat) {
        if ((date == null) || (newFormat == null)) {
            return null;
        }

        SimpleDateFormat formatter = new SimpleDateFormat(newFormat);
        return formatter.format(date);
    }

    public static Date formatString(String stringDate, String formatString) {
        if (stringDate == null || "".equals(stringDate)) {
            return null;
        }
        if (formatString == null || "".equals(formatString)) {
            formatString = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(formatString);

        try {
            return formatter.parse(stringDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取一个月的最后一天
     *
     * @param dat
     * @return
     */
    public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd
        String str = dat.substring(0, 8);
        String month = dat.substring(5, 7);
        int mon = Integer.parseInt(month);
        if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
            str += "31";
        } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
            str += "30";
        } else {
            if (isLeapYear(dat)) {
                str += "29";
            } else {
                str += "28";
            }
        }
        return str;
    }

    /**
     * 判断是否润年
     *
     * @param ddate
     * @return
     */
    public static boolean isLeapYear(String ddate) {

        /**
         * 详细设计： 1.被400整除是闰年，否则： 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
         * 3.能被4整除同时能被100整除则不是闰年
         */
        Date d = strToDate(ddate);
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(d);
        int year = gc.get(Calendar.YEAR);
        if ((year % 400) == 0)
            return true;
        else if ((year % 4) == 0) {
            if ((year % 100) == 0)
                return false;
            else
                return true;
        } else
            return false;
    }

    /**
     * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
     *
     * @param strDate
     * @return
     */
    public static Date strToDateLong(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    /**
     * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
     *
     * @param dateDate
     * @return
     */
    public static String dateToStrLong(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    /**
     * 将短时间格式时间转换为字符串 yyyy-MM-dd
     *
     * @param dateDate
     * @param k
     * @return
     */
    public static String dateToStr(Date dateDate) {
        return new SimpleDateFormat("yyyy-MM-dd").format(dateDate);
    }

    /**
     * 将短时间格式时间转换为字符串 yyyy-MM-dd
     *
     * @param dateDate Date
     * @param pattern  格式化模式
     * @return String 日期字符串
     */
    public static String dateToStr(Date dateDate, String pattern) {
        return new SimpleDateFormat(pattern).format(dateDate);
    }


    /**
     * 将短时间格式字符串转换为时间 yyyy-MM-dd
     *
     * @param strDate
     * @return
     */
    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    public static final String getDate(String pattern) {
        Date date = new Date();
        return getDate(date, pattern);
    }

    public static final String getDate(Date date, String pattern) {
        SimpleDateFormat df = null;
        String returnValue = "";
        if (date != null) {
            df = new SimpleDateFormat(pattern);
            returnValue = df.format(date);
        }
        return (returnValue);
    }
}
